package com.zyark.web.controller;

import com.zyark.web.util.AliyunOSSClientUtil;
import com.zyark.web.util.FasfdfsClientUtil;
import com.zyark.web.util.OSSClientConstants;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by ron on 17-4-24.
 */
@RequestMapping("file")
@Controller
public class FileController extends BaseController {

    Logger logger = LoggerFactory.getLogger(FileController.class);

//    @Resource
//    FasfdfsClientUtil fasfdfsClientUtil;

    @RequestMapping("upload")
    public void uploadFile(MultipartFile file, HttpServletRequest request, HttpServletResponse response){
        try {
            AliyunOSSClientUtil aliyunOSSClientUtil = new AliyunOSSClientUtil();
            String prefix = OSSClientConstants.CLIENT_ENDPOINT+OSSClientConstants.FOLDER;
            String filename = aliyunOSSClientUtil.uploadImg2Oss(file);
            String filePath =   AliyunOSSClientUtil.getImgUrl(filename);
            List<Map<String,String>> initialPreviewConfig = new ArrayList<Map<String, String>>();
            Map<String,String> p1 = new HashMap<>();
            p1.put("caption",filePath);
            p1.put("width","120px");
            p1.put("url","/file/delete");
            p1.put("key",filePath);
            p1.put("extra","{fileName:"+filePath+"}");
            initialPreviewConfig.add(p1);
            Map<String,List<Map<String,String>>> data = new HashMap<String,List<Map<String,String>>>();
            data.put("initialPreviewConfig",initialPreviewConfig);
            super.writeJson(data,response);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @RequestMapping("delete")
    @ResponseBody
    public Object deleteFile(@RequestParam String fileName){
        Map<String,String> result = new HashMap<>();
        try {
//            fasfdfsClientUtil.remove(fileName);

            return result.put("result","success");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result.put("result","fail");
    }
}
